// const reducers = combineReducers({
//   counter: counterReducer,
//   todos: todosReducer
// })

function combineReducers(reducers) {
  return function (state = {}, action) {
    let newState = {}
    for (let key in reducers) {
      const reducer = reducers[key]
      const prevState = reducer(state[key], action)
      newState[key] = prevState
    }
    return nextState
  }
}


function createStrore(reducer) {

  let state = {}
  let listener = []

  function subscribe(callback) {
    listener.push(callback)
  }

  function dispatch(action) {
    state = reducer(state, action)
    listener.forEach(item => item())
  }

  function getState() {
    return store
  }

  let store = {
    getState,
    subscribe,
    dispatch
  }

  // 初始化的时候，执行reducer
  state = reducer(state, {
    type: '@@init/redux.x.x.x'
  })

  return store
}

window.Redux = {}
Redux.createStore = createStrore
Redux.combineReducers = combineReducers